﻿using MediatR;
using W.Purchase.API.Application.Command.ReceivableCommand;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.ReceivableCommandHandler
{
    public class ReceivableUpdCommandHandler:IRequestHandler<ReceivableUpdCommand,int>
    {
        private readonly IReceivableRepository _receivableRepository;
        public ReceivableUpdCommandHandler(IReceivableRepository receivableRepository)
        {
            _receivableRepository = receivableRepository;
        }

        public async Task<int> Handle(ReceivableUpdCommand request, CancellationToken cancellationToken)
        {
            var list = await _receivableRepository.GetAllAsaync();
            var plist = list.Where(c => c.ReceivableId == request.ReceivableId).FirstOrDefault();
            if (plist != null)
            {
                plist.ReceivableId = request.ReceivableId;
                plist.ReceivableSerialNumber = request.ReceivableSerialNumber;
                plist.ReceivableName = request.ReceivableName;
                plist.FinanceTypeName = request.FinanceTypeName;
                plist.EndDate = request.EndDate;
                plist.Payer = request.Payer;
                plist.Payee = request.Payee;
                plist.ReceivableMoney = request.ReceivableMoney;
                plist.PracticalMoey = request.PracticalMoey;
                plist.ResidueMoney = request.ResidueMoney;
                plist.Status = request.Status;
                plist.PaymentNum = request.PaymentNum;
                plist.Receivableremark = request.Receivableremark;
                plist.office_id = request.office_id;
                plist.user_id = request.user_id;
                plist.CreateName = request.CreateName;
                plist.CreateTime = request.CreateTime;
                plist.UpdateName = request.UpdateName;
                plist.UpdateTime = DateTime.Now;
                plist.IsDelete = false;
            }
            return await _receivableRepository.Update(plist);
        }
    }
}
